﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VSA.Applications.VSA.WebService;

namespace VSA.Applications
{
    public partial class ActivityInfoForm : Form
    {
        MainService _ActivityService = WebServiceFactory.GetService();
        DataTable _ActivityDataSource = new DataTable();
        List<int> _DeletedIdList = new List<int>();
        public CompanyInfoForm CompanyForm { get; set; }

        public ActivityInfoForm()
        {
            InitializeComponent();

            gv_Activities.AutoGenerateColumns = false;
            _ActivityService.GetActivityListAsync("");
            _ActivityService.GetActivityListCompleted += new GetActivityListCompletedEventHandler(GetActivityListCompleted);
        }

        void GetActivityListCompleted(object sender, GetActivityListCompletedEventArgs e)
        {
            gv_Activities.DataSource = e.Result.Tables[0];
            _ActivityDataSource = gv_Activities.DataSource as DataTable;
        }

        void SubmitUpdate()
        {
            foreach (DataRow dr in _ActivityDataSource.Rows)
            {
                if (dr.RowState == DataRowState.Added)
                {
                    Activity activity = new Activity();
                    activity.ActivityContent = dr["ActivityContent"].ToString();
                    activity.CreateDate = DateTime.Now;
                    activity.Note = dr["Note"].ToString();
                    _ActivityService.AddActivity(activity);
                }
                else if (dr.RowState == DataRowState.Modified)
                {
                    Activity activity = new Activity();
                    activity.Did = int.Parse(dr["Did"].ToString());
                    activity.Note = dr["Note"].ToString();
                    activity.ActivityContent = dr["ActivityContent"].ToString();
                    _ActivityService.UpdateActivity(activity);
                }
                
                foreach (int did in _DeletedIdList)
                {
                    _ActivityService.DeleteActivity(did);
                }
                _DeletedIdList.Clear();
            }
            
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void txt_ActivityContent_Validated(object sender, EventArgs e)
        {
            TextBox txtBox = (TextBox)sender;
            this.errorProvider1.SetError(txtBox, "");
        }

        private void txt_ActivityContent_Validating(object sender, CancelEventArgs e)
        {
            string errorMsg = string.Empty;
            TextBox txtBox = (TextBox)sender;
            if (string.IsNullOrEmpty(txtBox.Text))
            {
                e.Cancel = true;
                txtBox.Select(0, txtBox.Text.Length);
                this.errorProvider1.SetError(txtBox, "Please input activity content.");
            }
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                SubmitUpdate();
                if (this.CompanyForm != null)
                {
                    DataTable dt = _ActivityService.GetActivityList("").Tables[0];
                    this.CompanyForm.BuildActivityBox(dt);
                    this.Dispose();
                }
                MessageBox.Show(
                    "Update values successfully",
                    "System Information",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information
                    );
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    ex.Message,
                    "System Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }

        private void gv_Activities_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            _DeletedIdList.Add(int.Parse(e.Row.Cells[0].Value.ToString()));
        }

        private void ActivityInfoForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
        }
    }
}
